<?php
//禁用错误报告
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);
require('config.php');
		
		$user_id=$_REQUEST['user_id'];
        //$user_id=60;
		$sql="select * from `".$oto."_car_session` where `userId`='".$user_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$person_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$car_session=unserialize($person_info[0]['car_session']);
		$i=0;
		foreach($car_session as $k=>$v)
		{
			//echo 'goodsName'.$v['goodsName']."<br/>";
			//echo 'goodsId'.$v['goodsId']."<br/>";
			$cart[$i]['goods_id']=$v['goodsId'];
			$cart[$i]['goods_num']=$v['cnt'];
			if($v['ischk']=="1" || $v['ischk']==1) $cart[$i]['isSelected']=true;
			else $cart[$i]['isSelected']=false;
			//$cart[$i]['isSelected']=$v['ischk'];
			$cart[$i]['isSelected']=$v['ischk'];
			$cart[$i]['goodsImg']=$v['goodsThums'];
			$cart[$i]['goodsName']=$v['goodsName'];
			$cart[$i]['shopPrice']=$v['shopPrice'];
			$cart[$i]['isGroup']=$v['isGroup'];
			$cart[$i]['isSeckill']=$v['isSeckill'];
			$cart[$i]['shopId']=$v['shopId'];
			$cart[$i]['shopName']=$v['shopName'];
			$cart[$i]['goods_attr']=0;
			$cart[$i]['goodss_attr_id']=$k;
			$cart[$i]['shopImg']=0;
			$cart[$i]['goodsStock']=0;
			$cart[$i]['shuxing_money']=0;
			$goodsId=$v['goodsId'];
			$sql="SELECT ".$oto."_goods.goodsStock,".$oto."_shops.shopImg FROM ".$oto."_goods,".$oto."_shops where ".$oto."_goods.shopId=".$oto."_shops.shopId and ".$oto."_goods.goodsId='{$goodsId}' LIMIT 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$goods_info[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			/*获取秒杀限购*/
			if($cart[$i]['isSeckill']==1 || $cart[$i]['isSeckill']=='1')
			{
				$time=time();
		        $t1=strtotime(date('Y-m-d 00:00:00',$time));
		        $t2=strtotime(date('Y-m-d 08:00:00',$time));
		        $t3=strtotime(date('Y-m-d 12:00:00',$time));
		        $t4=strtotime(date('Y-m-d 16:00:00',$time));
		        $t5=strtotime(date('Y-m-d 20:00:00',$time));
		        $t6=strtotime(date('Y-m-d 24:00:00',$time));
		        $data[0]['pan']=-1;
		        $goods_id=$v['goodsId'];
				if($time<$t2 && $time>=$t1)
				{
					$pan=1;
					$stat_time=$t1;
					$end_time=$t2;
				}
				if($time<$t3 && $time>=$t2)
				{
					$pan=2;
					$stat_time=$t2;
					$end_time=$t3;
				}
				if($time<$t4 && $time>=$t3)
				{
					$pan=3;
					$stat_time=$t3;
					$end_time=$t4;
				}
				if($time<$t5 && $time>=$t4)
				{
					$pan=4;
					$stat_time=$t4;
					$end_time=$t5;
				}
				if($time<$t6 && $time>=$t5)
				{
					$pan=5;
					$stat_time=$t5;
					$end_time=$t6;
				}
				$pan2=$pan-1;
		        $sql="select * from ".$oto."_goods,".$oto."_goods_seckill where ".$oto."_goods.isSeckill='1' and ".$oto."_goods.goodsId=".$oto."_goods_seckill.goodsId and ".$oto."_goods.shopId=".$oto."_goods_seckill.shopId and seckillStartTime<='{$time}' and seckillEndTime>'{$time}' and seckillSetTime='{$pan2}' and ".$oto."_goods.goodsId='{$goods_id}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$miaosha_info[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(!isset($miaosha_info[$i]))
				{
					$goods_info[$i]['seckillMaxCount']=0;
				}
				else
				{
					$goods_info[$i]['shopPrice']=$miaosha_info[$i][0]['seckillPrice'];
					$goods_info[$i]['seckillMaxCount']=$miaosha_info[$i][0]['seckillMaxCount'];
				}
			}
			/**/
			$cart[$i]['shopImg']=$goods_info[$i]['shopImg'];
			if($cart[$i]['isSeckill']==1 || $cart[$i]['isSeckill']=='1')
			{
				if($goods_info[$i]['goodsStock']<$goods_info[$i]['seckillMaxCount']) $cart[$i]['goodsStock']=$goods_info[$i]['goodsStock'];
				else $cart[$i]['goodsStock']=$goods_info[$i]['seckillMaxCount'];
			} 
			else $cart[$i]['goodsStock']=$goods_info[$i]['goodsStock'];
				$money=0;
				$goods_attr='';
				$attr_id=$k;
				$attr_id=explode("_",$attr_id); 
				// for($j=0;$j<count($attr_id);$j++)
				// {
				// 	echo count($attr_id[$j])."<br/>";
				// 	if($attr_id[$j]=="" || $attr_id[$j]==" " || $attr_id[$j]=="undefined") array_splice($attr_id, $j, 1);
				// }
				$lin=$attr_id;
				if(count($attr_id)-3==0) $attr_id=false;
				else
				{
					$attr_id='';
					for($j=1;$j<count($lin)-2;$j++)
					{
						if($j==1)
						{
							$attr_id.=$lin[$j];
						}
						else
						{
							$attr_id.=','.$lin[$j];
						}
					}
				}
				if($attr_id==false || $attr_id=='false')
				{
					$money=0;
					$goods_attr='';
				}
				else
				{
					$z_attr_money;
					$attr_id_list=explode(',',$attr_id);
					$attr_max=count($attr_id_list);
					for($j=0;$j<$attr_max;$j++)
					{
						$z_attr_id=$attr_id_list[$j];
						$sql="SELECT * FROM ".$oto."_goods_attributes where id='$z_attr_id'";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$z_attr_money[$j]=$row;//将取得的所有数据赋值给person_info数组
						}
						$money+=$z_attr_money[$j]['attrPrice'];
						if($j==0) $goods_attr.=$z_attr_money[$j]['attrVal'];
						else $goods_attr.=','.$z_attr_money[$j]['attrVal'];
						if($z_attr_money[$j]['attrStock']<$cart[$i]['goodsStock']) $cart[$i]['goodsStock']=$z_attr_money[$j]['attrStock'];
					}
				}
				$cart[$i]['shuxing_money']=$money;
				$cart[$i]['goods_attr']=$goods_attr;
			$i++;
		}
		//echo count($car_session);
		//print_r($car_session);
		//print_r($cart);
		if(isset($cart))
		{
			$shopId=-1;
			$shopNum=0;
			$shopId_list;
			$shopName_list;
			$shopImg_list;
			for($i=0;$i<count($cart);$i++)
			{
				if($cart[$i]['isSelected']=='1' || $cart[$i]['isSelected']==1) $cart[$i]['isSelected']=true;
				else $cart[$i]['isSelected']=false;
				if($cart[$i]['shopId']!=$shopId)
				{
					$pan_c_s=0;
					if(isset($shopId_list)) $lin_max=count($shopId_list);
					else $lin_max=0;
					for($j=0;$j<$lin_max;$j++)
					{
						if($shopId_list[$j]==$cart[$i]['shopId']) $pan_c_s=1;
					}
					if($pan_c_s==0)
					{
						$shopId_list[$shopNum]=$cart[$i]['shopId'];
						$shopName_list[$shopNum]=$cart[$i]['shopName'];
						$shopImg_list[$shopNum]=$cart[$i]['shopImg'];
						$shopId=$cart[$i]['shopId'];
						$shopNum++;
					}
				}
			}
			for($i=0;$i<$shopNum;$i++)
			{
				$t=0;
				$data[$i]['shopId']=$shopId_list[$i];
				$data[$i]['shopName']=$shopName_list[$i];
				$data[$i]['shopImg']=$shopImg_list[$i];
				for($j=0;$j<count($cart);$j++)
				{
					if($cart[$j]['shopId']==$shopId_list[$i])
					{
						$data[$i]['goods_list'][$t]=$cart[$j];
						$t++;
					}
				}
			}
		}
		if(isset($data)) echo json_encode($data);
		else echo '[]';
?>